home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc.v08 / sph_object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  5.9 KB  |  245 lines

  1. #include "HEADERS.h"
  2. #include "sphigslocal.h"
  3. #include <stdlib.h>
  4.  
  5.  
  6. static MAT3hvec raw_origin = {0.0,0.0,0.0,1.0};
  7. static MAT3hvec xformed_origin;
  8.  
  9.  
  10.  
  11.  
  12. extern srgp__point srgp_pdc_points[], srgp_polygon_points[];
  13.  
  14.  
  15.  
  16. void OBJECT__init (view_spec *vs)
  17. {
  18.    if (vs->uvnVertices == NULL) {
  19.       ALLOC (vs->uvnVertices, MAT3hvec, ASSUMED_NUM_OF_OBJECTS, 0);
  20.       vs->vertexArraySize = ASSUMED_NUM_OF_OBJECTS;
  21.    }
  22.    vs->vertexCount = 0;
  23.  
  24.    vs->objects = NULL;
  25.    vs->objectTail = NULL;
  26.  
  27.    vs->curTraversalIndex = 0;
  28.    
  29.    if (vs->objectChunk == NULL)
  30.       vs->objectChunk = FALLOCnew_chunk();
  31.    else
  32.       FALLOCclear_chunk (vs->objectChunk);
  33.  
  34. }
  35.  
  36.  
  37.  
  38. static void
  39. AddVertices (view_spec *vs, matrix xform, MAT3hvec *points, int count)
  40. {
  41.    register i;
  42.  
  43.    /* Check to see if enough vertices remain in global array */
  44.    if ((vs->vertexCount + count) > vs->vertexArraySize) {
  45.       vs->vertexArraySize += ASSUMED_NUM_OF_OBJECTS;
  46.       vs->uvnVertices = 
  47.      (MAT3hvec*) 
  48.         realloc (vs->uvnVertices, 
  49.              (MALLOCARGTYPE)(vs->vertexArraySize*sizeof(MAT3hvec)));
  50.       if (vs->uvnVertices == NULL)
  51.      SPH__error (ERR_MALLOC);
  52.    }
  53.  
  54.    /* Copy all the vertices to end of the uvnVertices array, performing
  55.       transformation while doing so. */
  56.    for (i=0; i<count; i++)
  57.       MAT3mult_hvec (vs->uvnVertices[vs->vertexCount++], points[i], xform, 1);
  58. }
  59.  
  60.  
  61.  
  62. static void
  63. AddObjectToEndOfList (view_spec *vs, obj *babyobj)
  64. {
  65.    if (vs->objectTail) {
  66.       vs->objectTail->next = babyobj;
  67.       vs->objectTail = babyobj;
  68.    }
  69.    else 
  70.       vs->objects = vs->objectTail = babyobj;
  71.    vs->objectTail->next = NULL;
  72. }
  73.  
  74.  
  75. void
  76. OBJECT__addFillArea
  77.    (view_spec *vs, MAT3hvec *points, int count,
  78.     matrix xform, attribute_group *attrs)
  79. {
  80.    obj *babyobj;
  81.    int bias;
  82.    register j;
  83.    vector vec1, vec2;
  84.  
  85.    bias = vs->vertexCount;
  86.  
  87.    AddVertices (vs, xform, points, count);
  88.  
  89.    /* Create a single facet objects. */
  90.    babyobj = (obj*)
  91.       FALLOCalloc (vs->objectChunk, sizeof(obj), FALLOC__DONT_ZERO);
  92.    babyobj->type = objFace;
  93.    babyobj->attributes = *attrs;
  94.    babyobj->data.face.numPoints = count;    
  95.    babyobj->data.face.points = 
  96.       (vertex_index*)
  97.      FALLOCalloc (vs->objectChunk,
  98.               sizeof(vertex_index) * babyobj->data.face.numPoints,
  99.               FALLOC__DONT_ZERO);
  100.    for (j=0; j < babyobj->data.face.numPoints; j++)
  101.       babyobj->data.face.points[j] = j + bias;
  102.  
  103.    /* Compute normal */
  104.    MAT3_SUB_VEC (vec1, 
  105.          vs->uvnVertices[bias+1], vs->uvnVertices[bias]);
  106.    MAT3_SUB_VEC (vec2, 
  107.          vs->uvnVertices[bias+2], vs->uvnVertices[bias+1]);
  108.    MAT3cross_product (babyobj->normal, vec1, vec2);
  109.    babyobj->normal[3] = 1.0;
  110.  
  111.    babyobj->traversal_index = vs->curTraversalIndex++;
  112.  
  113.    AddObjectToEndOfList (vs, babyobj);
  114. }
  115.  
  116.  
  117.  
  118.  
  119. void OBJECT__addPoly 
  120.    (view_spec *vs, POLYHEDRON *poly, matrix xform, attribute_group *attrs)
  121. {
  122.    obj *babyobj;
  123.    MAT3hvec xformed_normal;
  124.    register i;
  125.    int bias;
  126.  
  127.    bias = vs->vertexCount;
  128.  
  129.    AddVertices (vs, xform, poly->vertex_list, poly->vertex_count);
  130.  
  131.    /* For each face, create a new object. */
  132.    for (i=0; i<poly->facet_count; i++) {
  133.       register j;
  134.  
  135.       babyobj = (obj*)
  136.      FALLOCalloc (vs->objectChunk, sizeof(obj), FALLOC__DONT_ZERO);
  137.       babyobj->type = objFace;
  138.       babyobj->attributes = *attrs;
  139.       babyobj->data.face.numPoints = poly->facet_list[i].vertex_count;
  140.       babyobj->data.face.points = 
  141.      (vertex_index*)
  142.         FALLOCalloc (vs->objectChunk,
  143.              sizeof(vertex_index) * babyobj->data.face.numPoints,
  144.              FALLOC__DONT_ZERO);
  145.       /* Compute normal */
  146.       MAT3mult_vec (babyobj->normal, poly->facet_list[i].normal, 
  147.             currentNormalMCtoUVNxform);
  148.  
  149.       for (j=0; j < babyobj->data.face.numPoints; j++)
  150.      babyobj->data.face.points[j] =
  151.         poly->facet_list[i].vertex_indices[j] + bias;
  152.  
  153.       babyobj->traversal_index = vs->curTraversalIndex;
  154.  
  155.       AddObjectToEndOfList (vs, babyobj);
  156.    }
  157.  
  158.    vs->curTraversalIndex++;
  159. }
  160.  
  161.  
  162.  
  163. void OBJECT__process (view_spec *vs)
  164. {
  165.    switch (currentRendermode) {
  166.     case WIREFRAME:
  167.       SPH__map_to_canon (vs);
  168.       SPH__cull (vs);
  169.       SPH__clip (vs);
  170.       SPH__generate_pdc_vertices (vs);
  171.       break;
  172.     case FLAT:
  173.     case LIT_FLAT:
  174.       SPH__map_to_canon (vs);
  175.       SPH__cull (vs);
  176.       SPH__clip (vs);
  177.       SPH__calc_intensity (vs);
  178.       SPH__zsort (vs);
  179.       SPH__generate_pdc_vertices (vs);
  180.       break;
  181.    }
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. void OBJECT__drawAll (view_spec *vs)
  189. {
  190.    register obj *curobj;
  191.    register int v;
  192.    vertex_index *viptr;
  193.    facet *facetptr;
  194.    int shade_number, face_color;
  195.  
  196.  
  197.    for (curobj=vs->objects; curobj; curobj = curobj->next) {
  198.       switch (curobj->type) {
  199.  
  200.        case objFace:
  201.      /* CONVERT ALL VERTICES INTO SRGP VERTEX FORMAT. */
  202.      for (v = 0; v < curobj->data.face.numPoints; v++)
  203.         srgp_pdc_points[v] = vs->pdcVertices[curobj->data.face.points[v]];
  204.  
  205.      /* IF NOT WIREFRAME: */
  206.      if (currentRendermode > WIREFRAME) {
  207.         face_color = curobj->attributes.interior_color;
  208.         if (currentRendermode == LIT_FLAT) {
  209.            if IS_A_FLEXICOLORINDEX(face_color) {
  210.              shade_number = 
  211.             fabs(curobj->intensity)*NUM_OF_SHADES_PER_FLEXICOLOR;
  212.              if (shade_number == NUM_OF_SHADES_PER_FLEXICOLOR)
  213.             shade_number--;
  214.              SRGP_setColor ( 
  215.            BASE_OF_SHADE_LUT_ENTRIES
  216.              + ((face_color-2)*NUM_OF_SHADES_PER_FLEXICOLOR)
  217.             + shade_number);
  218.            SRGP_setFillStyle (SOLID);
  219.         }
  220.         else {
  221.            SRGP_setColor (face_color);
  222.            SRGP_setBackgroundColor (1);
  223.            shade_number = fabs(curobj->intensity)*64;
  224.            if (shade_number == 64)  shade_number--;
  225.            SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE);
  226.            SRGP_setFillBitmapPattern (40 + shade_number);
  227.          }
  228.         }
  229.         /* Draw the interior. */
  230.         SRGP_fillPolygon (curobj->data.face.numPoints, srgp_pdc_points);
  231.      }
  232.  
  233.      /* Draw the edge. */
  234.      if (curobj->attributes.edge_flag == EDGE_VISIBLE) {
  235.         SRGP_setColor (curobj->attributes.edge_color);
  236.         SRGP_setLineWidth (curobj->attributes.edge_width);
  237.         SRGP_polygon (curobj->data.face.numPoints, srgp_pdc_points);
  238.      }
  239.  
  240.      break;
  241.       }
  242.    }
  243. }
  244.  
  245.